package com.zmzncs.lmtc.module.promotion.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.parkinfo.ParkInfoDTO;
import com.zmzncs.lmtc.module.promotion.dao.MonthRuleParkMapper;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_park.MonthRulePark;
import com.zmzncs.lmtc.module.promotion.pojo.month_rule_park.MonthRuleParkDTO;
import com.zmzncs.lmtc.module.promotion.service.MonthRuleParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-08-12
 */
@Service
public class MonthRuleParkServiceImpl extends ServiceImpl<MonthRuleParkMapper, MonthRulePark> implements MonthRuleParkService {

    @Autowired
    MonthRuleParkMapper monthRuleParkMapper;
    @Autowired
    ParkInfoDao parkInfoDao;

    /**
     * 查询月租规则的停车场
     */
    @Override
    public List<MonthRuleParkDTO> getMonthRuleParkList(String keyword) {
        //  根据参数获取停车场列表
        IPage<ParkInfoDTO> parkInfoDTOPage = parkInfoDao.getParkByParamList(new Page(1, 10), keyword);

        //  停车场月租规则
        List<MonthRuleParkDTO> monthRuleParkDTOList = new ArrayList<>();
        for(ParkInfoDTO parkInfoDTO: parkInfoDTOPage.getRecords()){
            //  根据停车场id查询月租规则
            MonthRuleParkDTO monthRuleParkDTO = monthRuleParkMapper.getMonthRuleParkByParkId(parkInfoDTO.getId());
            //  停车场是否有月租规则
            if(monthRuleParkDTO == null){
                monthRuleParkDTO.setHasMonthRule(0);
            } else {
                monthRuleParkDTO.setHasMonthRule(1);
            }
            monthRuleParkDTO.setParkId(parkInfoDTO.getId());
            monthRuleParkDTO.setParkName(parkInfoDTO.getParkName());
            monthRuleParkDTO.setParkAddress(parkInfoDTO.getParkAddress());
            monthRuleParkDTO.setLongitude(parkInfoDTO.getLongitude());
            monthRuleParkDTO.setLatitude(parkInfoDTO.getLatitude());

            monthRuleParkDTOList.add(monthRuleParkDTO);
        }

        return monthRuleParkDTOList;
    }

}
